package com.huat.sportmeetingmanagement.controller;

import com.github.pagehelper.PageInfo;
import com.huat.sportmeetingmanagement.annotation.PassToken;
import com.huat.sportmeetingmanagement.entity.Admin;
import com.huat.sportmeetingmanagement.entity.GeneralVO;
import com.huat.sportmeetingmanagement.entity.Player;
import com.huat.sportmeetingmanagement.entity.Record;
import com.huat.sportmeetingmanagement.entity.enumEntity.ErrorListEnum;
import com.huat.sportmeetingmanagement.service.PlayerService;
import com.huat.sportmeetingmanagement.service.RecordService;
import com.huat.sportmeetingmanagement.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("player")
public class PlayerController {
    @Autowired
    PlayerService playerService;
    @Autowired
    RecordService recordService;
    @GetMapping("/getPlayerById")
    public GeneralVO getPlayerById(Integer id){
        Player player = playerService.getPlayerById(id);
        return new GeneralVO(ErrorListEnum.E200,player);

    }
    @PostMapping("/getPlayers")
    public GeneralVO getPlayers(@RequestBody Player player){
        List<Player> players = playerService.getPlayers(player);
        PageInfo<Player> playerPageInfo = new PageInfo<>(players);
        return new GeneralVO(ErrorListEnum.E200,playerPageInfo);

    }
    @PostMapping("/updatePlayer")
    public GeneralVO updatePlayer(@RequestBody Player player){
        Integer result = 0;
        try{
            result = playerService.updatePlayer(player);}
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E60001,"该运动员已存在");
        }
        return new GeneralVO(ErrorListEnum.E200,result);
    }
    @PostMapping("/addPlayer")
    public GeneralVO addPlayer(@RequestBody Player player){
        Integer result = 0;
        try{
            result = playerService.addPlayer(player);}
        catch (Exception ex){
            return new GeneralVO(ErrorListEnum.E60001,"该运动员已存在");
        }
        return new GeneralVO(ErrorListEnum.E200,result);
    }
    @PostMapping("/login")
    @PassToken
    public GeneralVO login(@RequestBody Admin admin){
        if(playerService.login(admin)){
            String token= TokenUtil.sign(admin);
            HashMap<String,Object> hs=new HashMap<>();
            hs.put("token",token);
            hs.put("user",admin);
            return new GeneralVO(ErrorListEnum.E200,hs);
        }
        else
            return new GeneralVO(ErrorListEnum.E20006,"账号或密码错误");

    }
    @PostMapping("/resetPassword")
    public GeneralVO resetPassword(@RequestBody Admin admin){
        if(playerService.resetPassword(admin)){
            return new GeneralVO(ErrorListEnum.E200,"修改成功");
        }
        else
            return new GeneralVO(ErrorListEnum.E20006,"修改失败");

    }
    @DeleteMapping("/{ids}")
    public GeneralVO deletePlayers(@PathVariable String[] ids){
        Integer result = 0;
        for (String no :ids) {
            Record record = new Record();
            record.setPlayerNo(no);
            if (recordService.getRecordById(record)!=null)
                return new GeneralVO(ErrorListEnum.E20004,"有数据与运动员绑定");
        }
        result = playerService.deletePlayers(ids);
        return new GeneralVO(ErrorListEnum.E200,result);
    }

}